// vue  数组重写原型
const arrOriginPro = Array.prototype
const arrNewPro = Object.create(arrOriginPro)
const arrMethods = ['push', 'pop', 'shift', 'unshift', 'splice', ]
arrMethods.forEach((met) => {
    arrNewPro[met] = function () {
        console.log('数组通知')
        update()
        arrOriginPro[met].apply(this, arguments)
    }
})

function isObj(obj) {
    return typeof obj === 'object'
}

function update() {
    console.log('数据更新')
}

function observe(obj) {
    if (!isObj(obj)) return;
    if (Array.isArray(obj)) {
        Object.setPrototypeOf(obj, arrNewPro)
    } else {
        const keys = Object.keys(obj)
        for (let index = 0; index < keys.length; index++) {
            const key = keys[index];
            const val = obj[key]
            reactive(obj, key, val)
        }
    }
}

function reactive(obj, key, val) {
    observe(val)
    Object.defineProperty(obj, key, {
        get() {
            console.log(`获取:${key}==>值为:${val}`)
            return val
        },
        set(newVal) {
            if (newVal !== val) {
                update()
                observe(newVal)
                console.log(`设置:${key}==>值为:${newVal}`)
                return val = newVal
            }
        }
    })
}
var data = {
    a: 1,
    b: 2,
    c: []
}
observe(data)